package com.houtai.controller.problem;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.problem.ProblemDetailDto;
import com.houtai.bean.dto.problem.ProblemListDto;
import com.houtai.bean.vo.problem.ContactDetailVo;
import com.houtai.bean.vo.problem.ProblemListVo;
import com.houtai.bean.vo.problem.TsProblemDetailVo;
import com.houtai.service.problem.ProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="常见问题接口", tags={"常见问题接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class ProblemContorller {
    @Autowired
    private ProblemService problemService;


    @Authority(value = AuthorityType.NoValidate)
    @ApiOperation(value="联系客服", notes="联系客服")
    @RequestMapping(value = "/problem/contact", method = RequestMethod.POST)
    public ContactDetailVo type() {
        return problemService.contactDetail();
    }

    @ApiOperation(value="常见问题列表", notes="常见问题列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/problem/list")
    public ProblemListVo list(@ApiParam(value = "常见问题列表所需参数") @Valid @RequestBody ProblemListDto dto, BindingResult result) {
        ProblemListVo retInfo = new ProblemListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.problemList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="常见问题详情", notes="常见问题详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/problem/detail")
    public TsProblemDetailVo detail(@ApiParam(value = "常见问题详情所需参数") @Valid @RequestBody ProblemDetailDto dto, BindingResult result,
                                    @RequestHeader String token) {
        TsProblemDetailVo retInfo = new TsProblemDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.problemDetail(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="常见问题满意", notes="常见问题满意")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/problem/statisfied")
    public ResultVo statisfied(@ApiParam(value = "常见问题满意所需参数") @Valid @RequestBody ProblemDetailDto dto, BindingResult result,
                           @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.updateTsProblemSatisfiedSize(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="常见问题不满意", notes="常见问题不满意")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/problem/statisfied/no")
    public ResultVo no(@ApiParam(value = "常见问题不满意所需参数") @Valid @RequestBody ProblemDetailDto dto, BindingResult result,
                               @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = problemService.updateTsProblemNoSatisfiedSize(dto);
        }
        return retInfo;
    }
}
